#ifndef _UDC_H_
#define _UDC_H_

#include "compiler.h"
#include "uotghs_device.h"
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif  

#define  MSC_CLASS                  0x08
#define  MSC_SUBCLASS_TRANSPARENT   0x06
#define  MSC_PROTOCOL_BULK          0x50 
  
#define  APPLE_DEVICE_CLASS         0xff
#define  IAP2_SUBCLASS_TRANSPARENT  0xf0
#define  DEFAULT_PROTOCOL           0x00
#define  NATIVE_PROTOCOL            0x01 
  
#define  MSC_EP_IN                  0x81
#define  MSC_EP_OUT                 0x02
#define  NATIVE_EP_IN               0x81
#define  NATIVE_EP_OUT              0x02
#define  IAP2_EP_IN                 0x84
#define  IAP2_EP_OUT                0x03
#define  INTR_EP_IN                 0x85

#define INTR_IN_EP_SIZE              64      
#define IAP2_PACKET_LEN             512
#define HS_BULK_EP_SIZE             512
#define FS_BULK_EP_SIZE              64

#define MFI_NO_AUTH                  0
#define MFI_AUTH_START               1
#define MFI_AUTH_COMPLETE            2  
  
#define USBD_MANUFACTURER_STRING      "MOPHIE"                         
//#define USBD_PRODUCT_STRING           "Juice Pack Memory"      
#define USBD_PRODUCT_STRING           "Space Pack" 
//#define USBD_SERIALNUMBER_HS_STRING   "00000000001A"                
#define USBD_CONFIGURATION_STRING     "MSC Config"
#define USBD_INTERFACE_IAP2_STRING    "iAP Interface"
#define USBD_INTERFACE_EA_STRING      "com.mophie.jpm"
#define USBD_MODEL_STRING             "communication"
#define USBD_CURRENT_LANG_STRING      "en"
#define USBD_SUPPORT_LANG_STRING      "en"
#define APP_BUNDLE_ID                 "com.mophie.space"
  
enum str_dsc_index
{
    DEFAULT_STR_IDX = 0,
    VENDOR_STR_IDX,
    PRODUCT_STR_IDX,
    SN_STR_IDX,
    CFG_STR_IDX,
    INTERFACE0_STR_IDX,
    INTERFACE1_STR_IDX,
};

#define set_sn_for_pc()              dev_dsc[16] = 0
#define set_sn_for_iph()             dev_dsc[16] = SN_STR_IDX

#define convert_cfg_dsc_fs()        cfg_dsc_msc[9 + 9 + 4] = 0x40;\
                                    cfg_dsc_msc[9 + 9 + 5] = 0x00;\
                                      cfg_dsc_msc[9 + 9 + 7 + 4] = 0x40;\
                                        cfg_dsc_msc[9 + 9 + 7 + 5] = 0x00
                                        
#define convert_cfg_dsc_hs()        cfg_dsc_msc[9 + 9 + 4] = 0x00;\
                                    cfg_dsc_msc[9 + 9 + 5] = 0x02;\
                                      cfg_dsc_msc[9 + 9 + 7 + 4] = 0x00;\
                                        cfg_dsc_msc[9 + 9 + 7 + 5] = 0x02
                                        
#define convert_cfg_otherspeed()           cfg_dsc_msc[1] = USB_DT_OTHER_SPEED_CONFIGURATION
#define convert_otherspeed_cfg()           cfg_dsc_msc[1] = USB_DT_CONFIGURATION
  
extern uint8_t g_msc_start;
extern uint8_t g_jpm_iap_start;
extern uint8_t g_jpm_native_start;
extern uint8_t g_jpm_start_stop_charge;
extern uint8_t g_jpm_start_mfi;

extern uint16_t msc_bulk_size;

//extern void   (*ep4_in_ptr)(void);
//extern void   (*ep3_out_ptr)(void);
  
void usb_device_interrupt(void);
//bool udd_ctrl_interrupt(void);  
bool jpm_setup(void);
uint16_t jpm_get_descriptor(void);
bool jpm_get_string_dsc(void);
void init_msc(void);
void init_jpm(void);
void init_jpm_iOS6(void);
void close_msc(void);
void close_jpm(void);
void init_native(void);
void close_native(void);
  
#endif // _UDC_H_
